package cloud.dwz.controller;

import cloud.dwz.entity.SysAreaEntity;
import cloud.dwz.service.SysAreaService;
import io.wit.common.utils.PageUtils;
import io.wit.common.utils.Query;
import io.wit.common.utils.R;
import io.wit.tool.ToolClient;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 省市县区域
*/
@RestController
@RequestMapping("sysarea")
public class SysAreaController {

    @Autowired
	private SysAreaService sysAreaService;

	/**
	 * 列表
	*/
	@GetMapping("/list")
	@RequiresPermissions("sysarea:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        final Query query = new Query(params);
		final List<SysAreaEntity> sysAreaList = sysAreaService.queryList(query);
		final int total = sysAreaService.queryTotal(query);
		final PageUtils page = new PageUtils(sysAreaList, total,query.getLimit(),query.getPage());
		return R.ok().put("page", page);
	}

	/**
	 * 获取详情信息
	*/
	@GetMapping("/info/{kid}")
    @RequiresPermissions("sysarea:info")
	public R info(@PathVariable("kid") Long kid){
		final SysAreaEntity sysArea = sysAreaService.queryObject(kid);
        if(sysArea != null){
            return R.ok().put("sysArea",sysArea);
        }else{
            return R.fail("暂无数据");
        }
	}

	/**
	 * 保存
	*/
	@PostMapping("/save")
	@RequiresPermissions("sysarea:save")
	public R save(@RequestBody SysAreaEntity sysArea){
        final String name = sysArea.getName();
        if(name == null || name.length() <=0){
            return R.fail("请输入区域名称!");
        }
        final Integer level = sysArea.getLevel();
        if(level == 0 || level == 1){
            sysArea.setLevel(1);
        }else{
            final Long pid = sysArea.getPid();
            if(pid == null){
                return R.fail("请选择父节点!");
            }
        }
        final Long pid = sysArea.getPid();
        if(pid == null || pid == 0){
            sysArea.setPid(0L);
        }else{
            final Integer l = sysArea.getLevel();
            if(l == 1){
                return R.fail("请取消父节点!");
            }
        }
        final Long pid1 = sysArea.getPid();
        if(pid1 == 0){
            final Integer l = sysArea.getLevel();
            if(l != 1){
                return R.fail("区域级别有误!");
            }
        }
        final int rows = sysAreaService.save(sysArea);
        return R.execute(rows);
	}

	/**
	 * 修改
	*/
	@PostMapping("/update")
	@RequiresPermissions("sysarea:update")
	public R update(@RequestBody SysAreaEntity sysArea){
        final String name = sysArea.getName();
        if(name == null || name.length() <=0){
            return R.fail("请输入区域名称!");
        }
        final Integer level = sysArea.getLevel();
        if(level == 0 || level == 1){
            sysArea.setLevel(1);
        }else{
            final Long pid = sysArea.getPid();
            if(pid == null){
                return R.fail("请取消父节点!");
            }
        }
        final Long pid = sysArea.getPid();
        if(pid == null || pid == 0){
            sysArea.setPid(0L);
        }else{
            final Integer l = sysArea.getLevel();
            if(l == 1){
                return R.fail("请取消父节点!");
            }
        }
        final Long pid1 = sysArea.getPid();
        if(pid1 == 0){
            final Integer l = sysArea.getLevel();
            if(l != 1){
                return R.fail("区域级别有误!");
            }
        }
        final int rows = sysAreaService.update(sysArea);
        return R.execute(rows);
	}

	/**
	 * 删除
	*/
	@PostMapping("/delete")
	@RequiresPermissions("sysarea:delete")
	public R delete(@RequestBody Long[] kids){
        final int rows = sysAreaService.deleteBatch(kids);
        return R.execute(rows);
	}

	/**通过父级id获取区域信息*/
    @GetMapping("/getListArea")
    public void getListArea(final Long pId,final HttpServletResponse response){
        ToolClient.responseJson(sysAreaService.getListArea(pId),response);
    }

    @GetMapping("/queryAreaTree")
    public void queryAreaTree(final Long kid,final HttpServletResponse response){
        ToolClient.responseJson(sysAreaService.queryAreaTree(kid),response);
    }
}